import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    realname: '',
    userId: '',
    roles: [],
    gender: '',
    telephone: '',
    birth: '',
    status: ''
  }
}
const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_REALNAME: (state, realname) => {
    state.realname = realname;
  },
  SET_ID: (state, id) => {
    state.userId = id;
  },
  SET_ROLES: (state, roleNames) => {
    state.roles = roleNames;
  },
  SET_REGISERTIME: (state, registerTime) => {
    state.registerTime = registerTime;
  },
  SET_ADDRESS: (state, address) => {
    state.address = address;
  },
  SET_GENDER: (state, gender) => {
    state.gender = gender;
  },
  SET_TELEPHONE: (state, telephone) => {
    state.telephone = telephone;
  },
  SET_BIRTH: (state, birth) => {
    state.birth = birth;
  },
  SET_STATUS: (state, status) => {
    state.status = status;
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        // const { name, avatar } = data
        const { username, userFace, realname, id, roles, registerTime, address, gender, telephone, birth, status } = data
        commit('SET_NAME', username)
        commit('SET_AVATAR', userFace)
        commit('SET_REALNAME', realname)
        commit('SET_ID', id)
        commit('SET_GENDER', gender)
        commit('SET_TELEPHONE', telephone)
        commit('SET_BIRTH', birth)
        commit('SET_STATUS', status)
        // 将角色对象数组转换为角色名称数组
        const roleNames = roles.map(role => role.name);
        // console.log(roleNames, '11111111111111111111111');
        // console.log(roles);

        commit('SET_ROLES', roleNames)
        commit('SET_REGISERTIME', registerTime)
        commit('SET_ADDRESS', address)



        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}